import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:simple_app/common/api.dart';


class RegPage extends StatefulWidget {
  const RegPage({Key? key}) : super(key: key);

  @override
  State<RegPage> createState() => _RegPageState();
}

class _RegPageState extends State<RegPage> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  final user = TextEditingController();
  final pwd = TextEditingController();
  final name = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  Widget textbox(String text, TextEditingController controller) {
    var txt = '请输入$text';
    return TextFormField(
      controller: controller,
      decoration: InputDecoration(
          focusedBorder: InputBorder.none,
          enabledBorder: InputBorder.none,
          errorBorder: InputBorder.none,
          disabledBorder: InputBorder.none,
          // hintStyle: const TextStyle(color: Color.fromARGB(255, 255, 255, 255)),
          hintText: txt),
      validator: (String? value) {
        if (value == null || value.isEmpty) {
          return '请填写$text';
        }
        return null;
      },
    );
  }

  var issubmit = false;

  Container regbtn(BuildContext ctx) {
    return Container(
      padding: const EdgeInsets.fromLTRB(0, 15, 0, 0),
      child: ElevatedButton(
          style: ButtonStyle(
              padding: MaterialStateProperty.all(
                  const EdgeInsets.fromLTRB(0, 15, 0, 15))),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              Text('注 册'),
            ],
          ),
          onPressed: () {
            if (_formKey.currentState!.validate()) {
              if (issubmit) {
                return;
              }
              issubmit = true;

              Api().reg(user.text, pwd.text, name.text).then((response) {
                if (response == null) {
                  ScaffoldMessenger.of(ctx).showSnackBar(
                    const SnackBar(content: Text('服务器错误')),
                  );
                  issubmit = false;
                  return;
                }
                if (response.statusCode != 200) {
                  ScaffoldMessenger.of(ctx).showSnackBar(
                    const SnackBar(content: Text('服务器错误')),
                  );
                  issubmit = false;
                  return;
                }
                issubmit = false;
                var data = jsonDecode(response.body);
                if (data['status'] != 1) {
                  ScaffoldMessenger.of(ctx).showSnackBar(
                    SnackBar(content: Text(data['info'])),
                  );
                } else {
                  //注册成功
                  ScaffoldMessenger.of(ctx).showSnackBar(
                    const SnackBar(content: Text('注册成功')),
                  );
                  Navigator.pop(ctx);
                  // Navigator.push(ctx,
                  //     MaterialPageRoute(builder: (context) => const TabList()));
                }
              });
              // var txt = '请求中... ${user.text} ${pwd.text}';
              // ScaffoldMessenger.of(context).showSnackBar(
              //   SnackBar(content: Text(txt)),
              // );

              //jump

            }
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('用户注册'),
      ),
      body: Container(
        alignment: Alignment.topCenter,
        child: Container(
            // height: 200,
            decoration: const BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(5)),
                color: Color.fromARGB(255, 255, 255, 255)),
            margin: const EdgeInsets.fromLTRB(60, 100, 60, 0),
            padding: const EdgeInsets.fromLTRB(10, 8, 10, 8),
            child: Form(
                key: _formKey,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    textbox('昵称', name),
                    textbox('用户名', user),
                    textbox('密码', pwd),
                    regbtn(context)
                  ],
                ))),
      ),
    );
  }
}
